//
//  WDLoginViewModel.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/21.
//

#import "WDLoginViewModel.h"

@implementation WDLoginViewModel

- (void)initialize {
    [super initialize];
    
    _reqModel = [WDAppLoginReqModel new];
    _reqModel.deviceType = @(1); // 1 表示 iOS
    ZHCustomUserInfoModel *userInfo =[[EDSAppDelegateTool sharedInstance] getUserInfo];
    if (userInfo && ![objectNull(userInfo.deviceId) isEqualToString:@""]) {
        _reqModel.deviceId = userInfo.deviceId;
    } else {
        _reqModel.deviceId = [EDSAppDelegateTool sharedInstance].getUUID;
    }
    
}

/// 登录
/// - Parameter completion: completion description
- (void)requestWithLogin:(RequestCompletionBlock)completion {
    @weakify(self);
    WDAppLoginRequest *request = [[WDAppLoginRequest alloc] initWithModel:self.reqModel];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        EDSRespondDataEntity *respond = request.responseModelObject;
//        [SVProgressHUD showInfoWithStatus:respond.msg];
        if (respond.code == EDS_NETWORK_SUCCESS) {
            ZHCustomUserInfoModel *user = respond.data;
            user.deviceId = self.reqModel.deviceId;
            user.loginState = 1;
            [[EDSAppDelegateTool sharedInstance] saveUserInfo:user];
        }
        [self sendResponseObject:request.responseModelObject completion:completion];
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
//        [SVProgressHUD showInfoWithStatus:@"网络不通"];
        [self sendResponseObject:request.responseModelObject completion:completion];
    }];
}

@end
